<!-- 分类页面 -->
<template>
    <div class="top-category">
        <div class="container">
            <AppBread/>

            <!-- 全部分类 -->
            <CategoryAll :categoryAll="result.children"/>

            <!-- 各个分类 -->
            <CategoryGoods v-for="item in result.children" :key="item.id"
            :title="item.name"
            :goods="item.goods"
            />
        </div>
    </div>
</template>

<script>
import AppBread from '@/components/AppBread.vue';
import { category } from '@/api/category';
import CategoryAll from './components/category-all';
import CategoryGoods from './components/category-goods';
export default{
    name:'category-main',
    components:{
        AppBread,
        CategoryAll,
        CategoryGoods
    },
    data(){
        return {
            result:{}
        }
    },
    watch:{
        // 这一步是，观察route对象的变化。如果有变化会触发handler回调
        $route:{
            immediate:true,//表示会立即触发一次
            handler(){
                this.result={}
                //获取根据传过来的分类id，获取对应分类数据
                category(this.$route.params.id).then(data=>{
                    this.result=data.data.result
                    this.bread()
                })
            }
        }
    },
    methods:{
        bread(){
            console.log(this.result.id);
            //通过全局事件总线，向面包屑组件发送数据
            this.$bus.$emit('bread',[
                {
                    id:'/category/'+this.result.id,
                    name:this.result.name
                }
            ])
        }
    }
}
</script>

<style lang="less">
.top-category{
    background-color: #f5f5f5;
    h3{
        text-align: center;
        font-size: 28px;
        font-weight: 400;
        line-height: 100px;
        color: #666;
    }
}
</style>